<?php

namespace app\admin\service;

use app\common\model\trAirportLine;
use app\common\model\trCarLine;
use app\common\model\trDriver;
use app\common\model\trBusesLine;
use app\common\model\trVehicleLine;
use app\common\util\ErrorCode;

/**
 * Class Auth
 * @package app\admin\service
 * 线路服务类
 */
class LineService
{
    private $returnData;
    private $busesLineField = [
        "name" => "require", "province_code_from" => "require", "city_code_from" => "0", "area_code_from" => "0", "address_from" => "require", "province_code_to" => "require", "date_between" => "",
        "city_code_to" => "0", "area_code_to" => "0", "address_to" => "require", "car_type" => "require", "default_adult_price" => "require", "weekend_adult_price" => "0", "date_adult_price" => "0",
        "default_children_price" => "require", "weekend_children_price" => "0", "date_children_price" => "0", "default_commission" => "require", "weekend_commission" => "0", "date_commission" => "0", "hot" => 1,
        "ride_number" => "require", "ticket_number" => "0", "driver" => "require", "shifts" => "require", "operation_time" => "require", "open_default" => 1, "open_weekend" => 0, "open_date" => 0,"ticket_deadline" => 0,
        "ticketing_time" => null
    ]; //填入数据库需要存储的表字段  巴士线路
    private $vehicleLineField = [
        "name" => "require", "province_code_from" => "require", "city_code_from" => "require", "area_code_from" => "0", "address_from" => "require", "province_code_to" => "require",
        "city_code_to" => "require", "area_code_to" => "0", "address_to" => "require", "car_type" => "require", "price" => "require", "hot" => 1
    ]; //填入数据库需要存储的表字段  包车线路
    private $airportLineField = [
        "name" => "require", "airport" => "require", "province_code" => "require", "city_code" => "require", "area_code" => "0", "address" => "", "charge_type" => 0,
        "car_type" => "require", "default_price" => "require", "weekend_price" => "0", "open_default" => 1, "open_weekend" => 0, "open_date" => 0, "date_between" => "",
        "date_price" => "0", "default_commission" => "require", "weekend_commission" => "0", "date_commission" => "0", "ride_number" => "0", "type" => "1"
    ]; //填入数据库需要存储的表字段  机场线路
    private $carLineField = [
        "name" => "require", "province_code_from" => "require", "city_code_from" => "0", "area_code_from" => "0", "address_from" => "", "province_code_to" => "require", "charge_type" => 0,
        "city_code_to" => "0", "area_code_to" => "0", "address_to" => "", "car_type" => "require", "default_price" => "require", "weekend_price" => "0", "date_price" => "0", "date_between" => '',
        "default_commission" => "0", "weekend_commission" => "0", "date_commission" => "0", "ride_number" => "0", "open_default" => 1, "open_weekend" => 0, "open_date" => 0
    ]; //填入数据库需要存储的表字段  专车线路
    private $fieldMsg = [
        "default_price" => "车票默认价格不能为空", "default_commission" => "车票默认佣金不能为空", "rideNumber" => "乘车人数不能为空",
        "default_adult_price" => "车票默认成人价格不能为空", "default_children_price" => "车票默认儿童价格不能为空", "price" => "车票价格不能为空"
    ];
    private $data = [];
    private $type;
    private $mall_id = 0;
    private $params;

    public function __construct($data, $type, $params = [])
    {
        $this->returnData = $data;
        $this->type = $type;
        $this->params = $params;
        $this->mall_id = request()->mallData["id"];
    }

    public function getList(){
        $page = isset($this->params['page']) ? $this->params['page'] : 1;
        $pageSize = isset($this->params['pageSize']) ? $this->params['pageSize'] : 10;
        //搜索条件
        $filter = array(["mall_id", "=", $this->mall_id]);
        if(isset($this->params['search'])){
            $filter[] = ['name', 'like', $this->params["search"] . "%"];
        }
        if($this->type == "enterprise"){ // 巴士路线线路
            $field = "id,name,car_type,province_code_from,city_code_from,area_code_from,address_from,province_code_to,city_code_to,
            area_code_to,address_to,default_adult_price,default_commission,ride_number,ticket_number,operation_time,status,hot";
            $model = new trBusesLine();
        }else if($this->type == "airport"){ // 机场线路
            $field = "id,name,car_type,province_code,city_code,area_code,airport,ride_number,default_price,default_commission,status,type";
            $model = new trAirportLine();
        }else if($this->type == "car"){ // 专车接送线路
            $field = "*";
            $model = new trCarLine();
        }else if($this->type == "vehicle") { // 包车线路
            $field = "id,province_code_from,city_code_from,area_code_from,address_from,province_code_to,city_code_to,area_code_to,address_to,
            name,car_type,price,status,hot";
            $model = new trVehicleLine();
        }else{
            return ErrorCode::$errorCode["fail"];
        }
        $data = $model->getList($field, $filter, $page, $pageSize);
        if(!$data["data"]){
            $this->returnData['data'] = ['list' => [], 'totalPage' => $page, 'currentPage' => $page, 'count' => 0];
        }else {
            if($this->type == "airport") { // 机场线路
                foreach ($data["data"] as $k => $item){
                    if($item["type"] == "1"){ // 接机
                        $data["data"][$k]["pcaFromText"] = $item["airportText"];
                        $data["data"][$k]["pcaToText"] = $item["pcaText"];
                    }else if($item["type"] == "2"){ // 送机
                        $data["data"][$k]["pcaFromText"] = $item["pcaText"];
                        $data["data"][$k]["pcaToText"] = $item["airportText"];
                    }
                    unset($data[$k]["pcaText"]);
                }
            }
            $this->returnData['data'] = ['list' => $data['data'], 'totalPage' => $data['last_page'], 'currentPage' => $data['current_page'], 'count' => $data['total']];
        }
        return $this->returnData;
    }

    public function toAdd(){
        switch ($this->type){
            case "enterprise":
                $field = $this->busesLineField;
                $model = new trBusesLine(); //巴士路线线路
                break;
            case "airport":
                $field = $this->airportLineField;
                $model = new trAirportLine(); //机场线路
                break;
            case "car":
                $field = $this->carLineField;
                $model = new trCarLine(); //专车接送线路
                break;
            case "vehicle":
                $field = $this->vehicleLineField;
                $model = new trVehicleLine(); //包车线路
                break;
            default:
                return ErrorCode::$errorCode["fail"];
                break;
        }
        foreach ($field as $k => $v) {
            $ks = parse_name($k); //转换字段，变驼峰
            if($v === 'require' && !isset($this->params[$ks])){
                $this->returnData = ErrorCode::code("params_not_exist", $ks);
                if(isset($this->fieldMsg[$k]))
                    $this->returnData["msg"] = $this->fieldMsg[$k];
                return $this->returnData;
            }
            $this->data[$k] = isset($this->params[$ks]) ? $this->params[$ks] : $v;
        }
        $result = $model->add($this->data);
        if(isset($result["code"])) {
            return $result;
        }
        if(!$result){
            $this->returnData = ErrorCode::$errorCode["fail"];
        }
        return $this->returnData;
    }

    public function toUpdate(){
        if(empty($this->params['id'])){
            return ErrorCode::code("params_not_exist", "id");
        }

        switch ($this->type){
            case "enterprise":
                $field = $this->busesLineField;
                $model = new trBusesLine(); //巴士路线线路
                break;
            case "airport":
                $field = $this->airportLineField;
                $model = new trAirportLine(); //机场线路
                break;
            case "car":
                $field = $this->carLineField;
                $model = new trCarLine(); //专车接送线路
                break;
            case "vehicle":
                $field = $this->vehicleLineField;
                $model = new trVehicleLine(); //包车线路
                break;
            default:
                return ErrorCode::$errorCode["fail"];
                break;
        }
        foreach ($field as $k => $v) {
            $ks = parse_name($k); //转换字段，变驼峰
            if($v === 'require' && !isset($this->params[$ks])){
                $this->returnData = ErrorCode::code("params_not_exist", $ks);
                if(isset($this->fieldMsg[$k]))
                    $this->returnData["msg"] = $this->fieldMsg[$k];
                return $this->returnData;
            }
            $this->data[$k] = isset($this->params[$ks]) ? $this->params[$ks] : $v;
        }
        $result = $model->doUpdate($this->data, ["id" => $this->params["id"], "mall_id" => $this->mall_id]);
        if(isset($result["code"])) {
            return $result;
        }
        if(!$result){
            $this->returnData = ErrorCode::$errorCode["fail"];
        }
        return $this->returnData;
    }

    public function getDetail(){
        if(empty($this->params['id'])){
            return ErrorCode::code("params_not_exist", "id");
        }
        switch ($this->type){
            case "enterprise":
                $model = new trBusesLine();
                $data = $model->getFields("*", ["id" => $this->params["id"], "mall_id" => $this->mall_id]);
                if(!empty($data["driver"])){
                    $data["driverText"] = '';
                    $driverModel = new trDriver();
                    foreach (explode(",", $data["driver"]) as $id){
                        $resData = $driverModel->getFields("nickname,mobile", ["id" => $id]);
                        $data["driverText"] .= ($resData["nickname"] . "（" . $resData["mobile"] . "）,");
                    }
                    $data["driverText"] = substr($data["driverText"], 0, -1);
                }
                break;
            case "airport":
                $model = new trAirportLine(); //机场线路
                $data = $model->getFields("*", ["id" => $this->params["id"], "mall_id" => $this->mall_id]);
                if(!empty($data)) {
                    if ($data["type"] == "1") { // 接机
                        $data["pcaFromText"] = $data["airportText"];
                        $data["pcaToText"] = $data["pcaText"];
                    } else if ($data["type"] == "2") { // 送机
                        $data["pcaFromText"] = $data["pcaText"];
                        $data["pcaToText"] = $data["airportText"];
                    }
                    unset($data["pcaText"]);
                }
                break;
            case "car":
                $model = new trCarLine(); //专车接送线路
                $data = $model->getFields("*", ["id" => $this->params["id"], "mall_id" => $this->mall_id]);
                break;
            case "vehicle":
                $model = new trVehicleLine(); //包车线路
                $data = $model->getFields("*", ["id" => $this->params["id"], "mall_id" => $this->mall_id]);
                break;
            default:
                return ErrorCode::$errorCode["fail"];
                break;
        }
        $this->returnData["data"] = $data;
        return $this->returnData;
    }

    public function toChangeStatus(){
        if(empty($this->params['id'])){
            return ErrorCode::code("params_not_exist", "id");
        }
        if(!isset($this->params['status'])){
            return ErrorCode::code("params_not_exist", "status");
        }
        if(!in_array($this->params["status"], [0,1])){ // 1展示， 0隐藏
            return ErrorCode::code("params_error", "status");
        }
        switch ($this->type){
            case "enterprise":
                $model = new trBusesLine();
                break;
            case "airport":
                $model = new trAirportLine(); //机场线路
                break;
            case "car":
                $model = new trCarLine(); //专车接送线路
                break;
            case "vehicle":
                $model = new trVehicleLine();
                break;
            default:
                return ErrorCode::$errorCode["fail"];
                break;
        }
        $this->data["status"] = $this->params["status"];
        $result = $model->doUpdate($this->data, ["id" => $this->params["id"], "mall_id" => $this->mall_id]);
        if(isset($result["code"])) {
            return $result;
        }
        if(!$result){
            $this->returnData = ErrorCode::$errorCode["fail"];
        }
        return $this->returnData;
    }

    public function toIsHot(){
        if(empty($this->params['id'])){
            return ErrorCode::code("params_not_exist", "id");
        }
        if(empty($this->params['hot'])){
            return ErrorCode::code("params_not_exist", "hot");
        }
        if(!in_array($this->params["hot"], [2,1])){ // 1正常， 0热门
            return ErrorCode::code("params_error", "hot");
        }
        switch ($this->type){
            case "enterprise": //巴士路线
                $model = new trBusesLine();
                break;
            case "vehicle":
                $model = new trVehicleLine(); //包车线路
                break;
            default:
                return ErrorCode::$errorCode["fail"];
                break;
        }
        $this->data["hot"] = $this->params["hot"];
        $result = $model->doUpdate($this->data, ["id" => $this->params["id"], "mall_id" => $this->mall_id]);
        if(isset($result["code"])) {
            return $result;
        }
        if(!$result){
            $this->returnData = ErrorCode::$errorCode["fail"];
        }
        return $this->returnData;
    }

    public function toDel(){
        if(empty($this->params['id'])){
            return ErrorCode::code("params_not_exist", "id");
        }
        switch ($this->type){
            case "enterprise":
                $model = new trBusesLine();
                break;
            case "airport":
                $model = new trAirportLine(); //机场线路
                break;
            case "car":
                $model = new trCarLine(); //专车接送线路
                break;
            case "vehicle":
                $model = new trVehicleLine(); //包车线路
                break;
            default:
                return ErrorCode::$errorCode["fail"];
                break;
        }
        $result = $model->delById($this->params['id']);
        if (isset($result["code"])) {
            return $result;
        }
        if (!$result) {
            $this->returnData = ErrorCode::$errorCode['fail'];
            $this->returnData["msg"] = "删除失败";
        }
        return $this->returnData;
    }
}